package com.miaoxun.controller;


import com.miaoxun.domain.enums.ErrEnum;
import com.miaoxun.common.util.Constant;
import com.miaoxun.domain.entity.Dept;
import com.miaoxun.domain.model.ResultModel;
import com.miaoxun.domain.model.Tree;
import com.miaoxun.service.DeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2017-09-27 14:40:36
 */

@Controller
@RequestMapping("/system/sysDept")
public class DeptController extends BaseController {
    private String prefix = "system/dept";

    @Autowired
    private DeptService sysDeptService;

    @GetMapping()
    @RequiresPermissions("system:sysDept:sysDept")
    String dept() {
        return prefix + "/dept";
    }

    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions("system:sysDept:sysDept")
    public List<Dept> list() {
        List<Dept> sysDeptList = sysDeptService.getAll();
        return sysDeptList;
    }

    @GetMapping("/add/{pId}")
    @RequiresPermissions("system:sysDept:add")
    String add(@PathVariable("pId") Long pId, Model model) {
        model.addAttribute("pId", pId);
        if (pId == 0) {
            model.addAttribute("pName", "总部门");
        } else {
            model.addAttribute("pName", sysDeptService.getModelById(pId).getName());
        }
        return prefix + "/add";
    }

    @GetMapping("/edit/{deptId}")
    @RequiresPermissions("system:sysDept:edit")
    String edit(@PathVariable("deptId") Long deptId, Model model) {
        Dept sysDept = sysDeptService.getModelById(deptId);
        model.addAttribute("sysDept", sysDept);
        if (Constant.DEPT_ROOT_ID.equals(sysDept.getParentId())) {
            model.addAttribute("parentDeptName", "无");
        } else {
            Dept parDept = sysDeptService.getModelById(sysDept.getParentId());
            model.addAttribute("parentDeptName", parDept.getName());
        }
        return prefix + "/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("system:sysDept:add")
    public ResultModel save(Dept sysDept) {
        if (sysDeptService.save(sysDept)!=null) {
            return ResultModel.SUCCESS(ErrEnum.SUCCESS.getCode(), ErrEnum.SUCCESS.getMsg());
        }
        return ResultModel.SUCCESS(ErrEnum.INSERT.getCode(), ErrEnum.INSERT.getMsg());
    }

    /**
     * 修改
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("system:sysDept:edit")
    public ResultModel update(Dept sysDept) {

        if (sysDeptService.update(sysDept)) {
            return ResultModel.SUCCESS(ErrEnum.SUCCESS.getCode(), ErrEnum.SUCCESS.getMsg());
        }
        return ResultModel.SUCCESS(ErrEnum.UPDATE.getCode(), ErrEnum.UPDATE.getMsg());
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("system:sysDept:remove")
    public ResultModel remove(Long deptId) {

        if (sysDeptService.count(deptId) > 0) {
            return ResultModel.SUCCESS(1, "包含下级部门,不允许删除", "");
        }
        if (sysDeptService.checkDeptHasUser(deptId) == 0) {
            if (sysDeptService.deleteById(deptId)) {
                return ResultModel.SUCCESS(ErrEnum.SUCCESS.getCode(), ErrEnum.SUCCESS.getMsg(), "");
            }
        } else {
            return ResultModel.SUCCESS(1, "部门包含用户", "");
        }
        return ResultModel.SUCCESS(1, "包含下级部门,不允许删除", "");
    }


    @GetMapping("/tree")
    @ResponseBody
    public Tree<Dept> tree() {
        Tree<Dept> tree = sysDeptService.getTree();
        return tree;
    }

    @GetMapping("/treeView")
    public String treeView() {
        return prefix + "/deptTree";
    }

}
